package com.yq.tanhua.ui

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.fmt.mvi.learn.gobal.log
import com.google.accompanist.swiperefresh.SwipeRefresh
import com.google.accompanist.swiperefresh.rememberSwipeRefreshState
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.yq.tanhua.ui.friends.FriendsViewModel
import com.yq.tanhua.ui.friends.RequestDataStatus
import com.yq.tanhua.ui.views.LoadingFooter

@SuppressLint("StateFlowValueCalledInComposition")
@Composable
fun FollowMovmentsPage(
    viewModel: FriendsViewModel
) {

    val friendsMovmentUiState = viewModel.friendsMovmentUiState.collectAsState()
    val movments = friendsMovmentUiState.value
//    "当前状态： ${friendsMovmentUiState.value.status}".log()
    if (friendsMovmentUiState.value.status == null) {
//        "好友动态页面  数据加载啦。。。".log()
        viewModel.getFriendsMovment(true)
        Text(text = "数据加载中....")
    } else {

        if (movments.status == RequestDataStatus.NoData) {
            Text(text = "暂无数据")
        } else {
            val refreshState =
                rememberSwipeRefreshState(movments.status == RequestDataStatus.RefreshIng)
            SwipeRefresh(state = refreshState, onRefresh = {
                viewModel.getFriendsMovment(true)
            }) {
                movments.datas?.let {
                    LazyColumn {
                        items(it.size) { index ->
                            val item = movments.datas[index]
                            MovementItemView(item)
                        }
                        item {
                            if (movments.status == RequestDataStatus.NoMoreData) {
                                Text(text = "暂无更多数据", Modifier.fillMaxWidth(1f))
                            } else {
                                LoadingFooter()
                                if (movments.status != RequestDataStatus.LoadMoreIng) {
                                    "获取更多数据".log()
                                    viewModel.getFriendsMovment(false)
                                }
                            }
                        }
                    }
                }
            }
        }
    }


}